

export class Http {

    public static sendJson(url: string, data: any, post: boolean) {
        return new Promise((resolve) => {
            let xhr = new XMLHttpRequest();
            xhr.timeout = 20000;
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    // console.log(response)
                    resolve?.(JSON.parse(response));
                } else if (xhr.readyState == 4 && xhr.status == 0) {
                    // errorCall?.('no_server');
                    console.error("no_server")
                } else if (xhr.status > 500) {
                    // errorCall?.('no_server');
                }
            };

            xhr.onerror = () => {
                console.error("no_server")
                // errorCall?.('no_server');
                // console.log('================protobufHttpRequest================onerror');
            }

            xhr.ontimeout = () => {
                console.error("no_server")
                // errorCall?.('no_server');
                // console.log('================protobufHttpRequest================ontimeout');
            }

            xhr.open(post ? "POST" : "GET", url, true);
            if (post) {
                xhr.setRequestHeader("Content-Type", "application/json");
                // xhr.setRequestHeader("sign", md5);
            } else {
                xhr.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0");
            }
            // console.log(data);
            xhr.send(JSON.stringify(data));
        })

    }
}



